package com.kk.permission;

import android.accessibilityservice.AccessibilityService;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Build.VERSION;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import com.kk.permission.huawei.HuaweiPermissionGuideStrategy;
import com.kk.permission.utils.BaseUtil;
import com.kk.permission.utils.OSUtil;
import com.kk.permission.utils.PackageUtil;

import java.util.List;
import java.util.Locale;

public class PermissionGuideUtil {
    private static final String TAG = "PermissionGuideUtil";
    private static Boolean sIsAutoPermission;

    public static boolean shouldShowStartUpGuide() {
        if (/*OSUtil.isMiuiV6() || OSUtil.isMiuiV7() || OSUtil.isMiuiV5() || OSUtil.isMiuiV8() ||*/ PackageUtil.isPackageInstalled("com.huawei.systemmanager") /*|| PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled("com.iqoo.secure")*/) {
            return true;
        }
//        if (!PackageUtil.isPackageInstalled("com.zte.heartyservice") || ZtePermissionGuideStrategy.getPermissionManagerVersion() < 1) {
//            return false;
//        }
        return true;
    }

    public static boolean shouldShowToastPermissionEntry() {
        return/* OSUtil.isMiuiV5() || */PackageUtil.isPackageInstalled("com.huawei.systemmanager")/* || PackageUtil.isPackageInstalled(PackageUtil.OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES) || ((PackageUtil.isPackageInstalled("com.iqoo.secure") && VivoPermissionGuideStrategyBase.getVivoManagerVersion() == 2) || PackageUtil.isPackageInstalled("com.meizu.safe"))*/;
    }

//    public static void launchSpecificPermissionGuide(Context context, int i) {
//        int i2 = 0;
//        if (i == 3) {
//            if (PackageUtil.isPackageInstalled(PackageUtil.OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES)) {
//                new OppoColorOSPermissionGuideStrategy(context, false).actionToastPermission();
//                return;
//            } else if (PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES)) {
//                new OppoPermissionGuideStrategy(context, false).actionToastPermission();
//                return;
//            }
//        }
//        Intent intent = new Intent(context, SpecificPermissionActivity.class);
//        List permissionList = PermissionGuideGenerator.generateGuideStratagy(context).getPermissionList();
//        if (permissionList != null && permissionList.size() > 0) {
//            String[] strArr = new String[permissionList.size()];
//            while (i2 < permissionList.size()) {
//                strArr[i2] = (String) permissionList.get(i2);
//                i2++;
//            }
//            intent.putExtra("permission_list", strArr);
//            intent.putExtra(PermissionGuideActivity.PERMISSION_LIST_TYPE, -1);
//            context.startActivity(intent);
//        }
//    }

//    public static void requestCalllogPermissionOnce(final ContentResolver contentResolver) {
//        if (PrefUtil.getKeyBoolean("first_request_calllog_permission", true)) {
//            new Thread(new Runnable() {
//                public void run() {
//                    try {
//                        Cursor query = contentResolver.query(CallLog.CONTENT_URI, null, null, null, "limit 1");
//                        if (query != null) {
//                            try {
//                                query.close();
//                            } catch (Exception unused) {
//                                PrefUtil.setKey("first_request_calllog_permission", false);
//                                return;
//                            }
//                        }
//                    } catch (Throwable e) {
//                        TLog.printStackTrace(e);
//                    }
//                }
//            }).start();
//        }
//    }

    public static String getMeizuSecVersion(Context context) {
        try {
            return context.getPackageManager().getPackageInfo("com.meizu.safe", 0).versionName;
        } catch (Exception e) {
            String str = TAG;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Failed to get Meizu sec version:");
            stringBuilder.append(e.toString());
            return "Failed to get Meizu sec version";
        }
    }

//    public static Intent getOuterTwoStepsAccessibilityIntent(Context context) {
//        boolean z = false;
//        if (PackageUtil.isPackageInstalled("com.meizu.safe")) {
//            String meizuSecVersion = getMeizuSecVersion(context);
//            if (meizuSecVersion == null || !meizuSecVersion.startsWith("2")) {
//                z = true;
//            }
//        }
//        return AnimStepsPermissionActivity.getIntent(context, z);
//    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static void traverseNode(AccessibilityNodeInfo accessibilityNodeInfo, int i) {
        String stringBuilder;
        int i2 = 0;
        String str = "";
        for (int i3 = 0; i3 < i; i3++) {
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append(str);
            stringBuilder2.append("   ");
            str = stringBuilder2.toString();
        }
        StringBuilder stringBuilder3;
        if (accessibilityNodeInfo == null) {
            stringBuilder3 = new StringBuilder();
            stringBuilder3.append(str);
            stringBuilder3.append("node=null");
            stringBuilder = stringBuilder3.toString();
        } else {
            stringBuilder3 = new StringBuilder();
            stringBuilder3.append(str);
            stringBuilder3.append("class=");
            stringBuilder3.append(accessibilityNodeInfo.getClassName() == null ? "null" : accessibilityNodeInfo.getClassName());
            stringBuilder = stringBuilder3.toString();
            StringBuilder stringBuilder4 = new StringBuilder();
            stringBuilder4.append(stringBuilder);
            stringBuilder4.append(",  id=");
            stringBuilder4.append(accessibilityNodeInfo.getViewIdResourceName() == null ? "null" : accessibilityNodeInfo.getViewIdResourceName());
            stringBuilder = stringBuilder4.toString();
            stringBuilder4 = new StringBuilder();
            stringBuilder4.append(stringBuilder);
            stringBuilder4.append(",  text=");
            stringBuilder4.append(accessibilityNodeInfo.getText() == null ? "null" : accessibilityNodeInfo.getText());
            stringBuilder = stringBuilder4.toString();
        }
        if (accessibilityNodeInfo != null) {
            while (i2 < accessibilityNodeInfo.getChildCount()) {
                traverseNode(accessibilityNodeInfo.getChild(i2), i + 1);
                i2++;
            }
        }
    }

    public static void dumpNode(AccessibilityService accessibilityService) {
        AccessibilityNodeInfo rootInActiveWindow = accessibilityService.getRootInActiveWindow();
        if (rootInActiveWindow != null) {
            traverseNode(rootInActiveWindow, 0);
        }
    }

    private static boolean enableDozeCheck() {
        return (OSUtil.isMiui() || OSUtil.isHuawei() || PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled(PackageUtil.OPPO_2_0_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled(PackageUtil.OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled("com.iqoo.secure") || PackageUtil.isPackageInstalled(PackageUtil.SAMSUNG_PERMISSION_SETTINGS_PACKAGE_NAMES) || PackageUtil.isPackageInstalled("com.meizu.safe")) ? false : true;
    }

    /* JADX WARNING: Removed duplicated region for block: B:38:0x00c9  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x00c9  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x00c9  */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x00a9 A:{Splitter: B:33:0x00a5, ExcHandler: android.content.ActivityNotFoundException (r9_1 'e' java.lang.RuntimeException)} */
    /* JADX WARNING: Removed duplicated region for block: B:35:0x00a9 A:{Splitter: B:33:0x00a5, ExcHandler: android.content.ActivityNotFoundException (r9_1 'e' java.lang.RuntimeException)} */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x00c9  */
    /* JADX WARNING: Removed duplicated region for block: B:38:0x00c9  */
    /* JADX WARNING: Missing block: B:35:0x00a9, code:
            r9 = move-exception;
     */
    /* JADX WARNING: Missing block: B:36:0x00aa, code:
            r2 = TAG;
            r5 = new java.lang.StringBuilder();
            r5.append("Failed to start android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS activity:");
            r5.append(r9.toString());
            com.cootek.base.tplog.TLog.i(r2, r5.toString(), new java.lang.Object[0]);
            r1 = 1;
     */
    public static void checkDozePermission(android.content.Context r9) {
        /*
        r0 = android.os.Build.VERSION.SDK_INT;
        r1 = 23;
        if (r0 < r1) goto L_0x0105;
    L_0x0006:
        r0 = enableDozeCheck();
        if (r0 == 0) goto L_0x0105;
    L_0x000c:
        r0 = "permission_guide_doze_check_times";
        r1 = 0;
        r0 = com.cootek.permission.utils.PrefUtil.getKeyInt(r0, r1);
        if (r0 >= 0) goto L_0x001f;
    L_0x0015:
        r0 = "PermissionGuideUtil";
        r2 = "invalid check times";
        r3 = new java.lang.Object[r1];
        com.cootek.base.tplog.TLog.w(r0, r2, r3);
        r0 = 0;
    L_0x001f:
        r2 = 2;
        if (r0 < r2) goto L_0x0023;
    L_0x0022:
        return;
    L_0x0023:
        r2 = r9.getPackageName();
        r3 = "power";
        r3 = r9.getSystemService(r3);
        r3 = (android.os.PowerManager) r3;
        r4 = 1;
        r5 = r3.getClass();	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r6 = "isIgnoringBatteryOptimizations";
        r7 = new java.lang.Class[r4];	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r8 = java.lang.String.class;
        r7[r1] = r8;	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r5 = r5.getMethod(r6, r7);	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        if (r5 == 0) goto L_0x006f;
    L_0x0042:
        r6 = new java.lang.Object[r4];	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r6[r1] = r2;	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r3 = r5.invoke(r3, r6);	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r3 = (java.lang.Boolean) r3;	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r3 = r3.booleanValue();	 Catch:{ IllegalAccessException -> 0x007d, InvocationTargetException -> 0x0077, NoSuchMethodException -> 0x0071 }
        r5 = "PermissionGuideUtil";
        r6 = new java.lang.StringBuilder;	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        r6.<init>();	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        r7 = "isIgnoringBatteryOptimizations=";
        r6.append(r7);	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        r6.append(r3);	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        r6 = r6.toString();	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        r7 = new java.lang.Object[r1];	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        com.cootek.base.tplog.TLog.i(r5, r6, r7);	 Catch:{ IllegalAccessException -> 0x006d, InvocationTargetException -> 0x006b, NoSuchMethodException -> 0x0069 }
        goto L_0x0082;
    L_0x0069:
        r5 = move-exception;
        goto L_0x0073;
    L_0x006b:
        r5 = move-exception;
        goto L_0x0079;
    L_0x006d:
        r5 = move-exception;
        goto L_0x007f;
    L_0x006f:
        r3 = 1;
        goto L_0x0082;
    L_0x0071:
        r5 = move-exception;
        r3 = 1;
    L_0x0073:
        com.cootek.base.tplog.TLog.printStackTrace(r5);
        goto L_0x0082;
    L_0x0077:
        r5 = move-exception;
        r3 = 1;
    L_0x0079:
        com.cootek.base.tplog.TLog.printStackTrace(r5);
        goto L_0x0082;
    L_0x007d:
        r5 = move-exception;
        r3 = 1;
    L_0x007f:
        com.cootek.base.tplog.TLog.printStackTrace(r5);
    L_0x0082:
        if (r3 != 0) goto L_0x00c7;
    L_0x0084:
        if (r0 != 0) goto L_0x00c7;
    L_0x0086:
        r5 = new android.content.Intent;
        r6 = "android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS";
        r5.<init>(r6);
        r6 = new java.lang.StringBuilder;
        r6.<init>();
        r7 = "package:";
        r6.append(r7);
        r6.append(r2);
        r2 = r6.toString();
        r2 = android.net.Uri.parse(r2);
        r5.setData(r2);
        r9.startActivity(r5);	 Catch:{ ActivityNotFoundException -> 0x00a9, ActivityNotFoundException -> 0x00a9, ActivityNotFoundException -> 0x00a9 }
        goto L_0x00c7;
    L_0x00a9:
        r9 = move-exception;
        r2 = "PermissionGuideUtil";
        r5 = new java.lang.StringBuilder;
        r5.<init>();
        r6 = "Failed to start android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS activity:";
        r5.append(r6);
        r9 = r9.toString();
        r5.append(r9);
        r9 = r5.toString();
        r1 = new java.lang.Object[r1];
        com.cootek.base.tplog.TLog.i(r2, r9, r1);
        r1 = 1;
    L_0x00c7:
        if (r1 == 0) goto L_0x00e3;
    L_0x00c9:
        r9 = new java.util.HashMap;
        r9.<init>();
        r1 = "event";
        r2 = "event_start_doze_permission_activity_fail";
        r9.put(r1, r2);
        r1 = "start_doze_permission_activity_fail";
        r2 = java.lang.Boolean.valueOf(r4);
        r9.put(r1, r2);
        r1 = "path_permission";
        com.cootek.dialer.base.stat.StatRecorder.record(r1, r9);
    L_0x00e3:
        r0 = r0 + r4;
        r9 = "permission_guide_doze_check_times";
        com.cootek.permission.utils.PrefUtil.setKey(r9, r0);
        r9 = new java.util.HashMap;
        r9.<init>();
        r1 = "event";
        r2 = "event_doze_permission_app_open_check";
        r9.put(r1, r2);
        r0 = java.lang.Integer.toString(r0);
        r1 = java.lang.Boolean.valueOf(r3);
        r9.put(r0, r1);
        r0 = "path_permission";
        com.cootek.dialer.base.stat.StatRecorder.record(r0, r9);
    L_0x0105:
        return;
        */
        //throw new UnsupportedOperationException("Method not decompiled: com.cootek.permission.PermissionGuideUtil.checkDozePermission(android.content.Context):void");
    }

    public static boolean nodeContainsViewId(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        List findAccessibilityNodeInfosByViewId = accessibilityNodeInfo.findAccessibilityNodeInfosByViewId(str);
        String str2;
        StringBuilder stringBuilder;
        if (findAccessibilityNodeInfosByViewId == null || findAccessibilityNodeInfosByViewId.size() <= 0) {
            str2 = TAG;
            stringBuilder = new StringBuilder();
            stringBuilder.append("page not contains id:");
            stringBuilder.append(str);
            return false;
        }
        str2 = TAG;
        stringBuilder = new StringBuilder();
        stringBuilder.append("page contains id:");
        stringBuilder.append(str);
        return true;
    }

    public static boolean nodeContainsText(AccessibilityNodeInfo accessibilityNodeInfo, String str) {
        List findAccessibilityNodeInfosByText = accessibilityNodeInfo.findAccessibilityNodeInfosByText(str);
        String str2;
        StringBuilder stringBuilder;
        if (findAccessibilityNodeInfosByText == null || findAccessibilityNodeInfosByText.size() <= 0) {
            str2 = TAG;
            stringBuilder = new StringBuilder();
            stringBuilder.append("page not contains text:");
            stringBuilder.append(str);
            return false;
        }
        str2 = TAG;
        stringBuilder = new StringBuilder();
        stringBuilder.append("page contains text:");
        stringBuilder.append(str);
        return true;
    }

    public static void sleep(long j) {
        try {
            Thread.sleep(j);
        } catch (InterruptedException unused) {
        }
    }

    public static AccessibilityNodeInfo findScrollableView(AccessibilityNodeInfo accessibilityNodeInfo) {
        if (accessibilityNodeInfo == null || accessibilityNodeInfo.getChildCount() == 0) {
            return null;
        }
        if (accessibilityNodeInfo.getClassName() != null && accessibilityNodeInfo.getClassName().equals("android.widget.ListView")) {
            return accessibilityNodeInfo;
        }
        for (int i = 0; i < accessibilityNodeInfo.getChildCount(); i++) {
            AccessibilityNodeInfo findScrollableView = findScrollableView(accessibilityNodeInfo.getChild(i));
            if (findScrollableView != null) {
                return findScrollableView;
            }
        }
        return null;
    }

    public static boolean isAutoPermission() {
        if (sIsAutoPermission == null) {
            sIsAutoPermission = Boolean.valueOf(queryAutoPermission());
        }
        return sIsAutoPermission.booleanValue();
    }

    private static boolean queryAutoPermission() {
//        if ((OSUtil.isSamsung6() || OSUtil.isSamsung7()) && !"SM-N9500".equals(Build.MODEL)) {
//            return !"SM-C9000".equals(Build.MODEL) || Build.TIME < 1504257263000L;
//        } else {
            if (!PackageUtil.isIntentAvailable(new Intent("android.settings.ACCESSIBILITY_SETTINGS"))) {
                return false;
            }
           /* if (PackageUtil.isPackageInstalled(PackageUtil.OPPO_COLOROS_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES) || PackageUtil.isPackageInstalled(PackageUtil.OPPO_2_0_PERMISSION_SETTING_PACKAGE_NAMES)) {
//                if (OppoColorOSPermissionGuideStrategy.isAutoPermission()) {
//                    return true;
//                }
                StringBuilder stringBuilder;
                try {
                    String str = Build.DISPLAY;
                    String str2 = TAG;
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("display=");
                    stringBuilder.append(str);
                    TLog.i(str2, stringBuilder.toString(), new Object[0]);
                    String[] split = str.split("_");
                    str = split[split.length - 1];
                    return (Integer.parseInt(str) < 170500 || Integer.parseInt(str) >= 170603) && Integer.parseInt(str) <= 170705;
                } catch (Exception e) {
                    stringBuilder = new StringBuilder();
                    stringBuilder.append(e);
                    stringBuilder.append("");
                    TLog.e("weyl", stringBuilder.toString(), new Object[0]);
                }
            } else*/ if (PackageUtil.isPackageInstalled("com.huawei.systemmanager")) {
                if (VERSION.SDK_INT >= 19 && new HuaweiPermissionGuideStrategy(BaseUtil.getAppContext()).supportAutoPermission()) {
                    return Locale.getDefault().getLanguage().equals("en") || (Locale.getDefault().getLanguage().equals("zh") && Locale.getDefault().getCountry().equals("CN"));
                } else {
                    return false;
                }
            }/* else if (OSUtil.isMiuiV6() || OSUtil.isMiuiV7()) {
                return false;
            } else {
                if (OSUtil.isMiuiV8()) {
                    if (Build.TIME < 1503128052000L && !VERSION.INCREMENTAL.startsWith("V8.2")) {
                        return Locale.getDefault().getLanguage().equals("en") || (Locale.getDefault().getLanguage().equals("zh") && Locale.getDefault().getCountry().equals("CN"));
                    } else {
                        return false;
                    }
                } *//*else if (!OSUtil.isMiuiV9() && PackageUtil.isPackageInstalled("com.meizu.safe")) {
                    return MeizuPermissionGuideStrategy.isAutoPermission();
                }*//* else {
                    return false;
                }
            }*/
//        }
        return false;
    }
}
